package com.zhong.gang.applib.http.callback;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.luck.picture.lib.tools.ToastUtils;
import com.zhong.gang.applib.utils.ActivityController;
import com.zhong.gang.applib.BaseApp;
import com.zhong.gang.applib.http.BaseDataBean;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.Response;

/**
 * Created by zhy on 15/12/14.
 */
public abstract class DataBaseBeanCallback<M extends BaseDataBean> extends Callback<String> {
    private Gson gson = new Gson();
    private Fragment fragment;
    private Activity activity;
    public Context context;
    public static LoginOutListener loginOutListener;

    public DataBaseBeanCallback() {
    }

    public DataBaseBeanCallback(Activity activity) {
        this.activity = activity;
        this.context = activity;
    }

    public DataBaseBeanCallback(Fragment fragment) {
        this.fragment = fragment;
        if (fragment != null)
        this.context = fragment.getContext();
    }

    @Override
    public String parseNetworkResponse(Response response, int id) throws IOException {
        String str = response.body().string();
        int logzie = 2000;
        if (str.length() > logzie) {
            for (int i = 0; i < str.length(); i += logzie) {
                if (i + logzie < str.length())
                    Log.i("==``", str.substring(i, i + logzie));
                else {
                    Log.i("==``", str.substring(i, str.length()));
                }
            }
        } else {
            Log.i("==``", "返回结果" + str);
        }
        return str;
    }

    @Override
    public void onResponse(String response, int id) {
        try {
            Type type = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
            M dataBean = gson.fromJson(response, type);

            if (dataBean.getCode() == 200)
                onResponse(dataBean);
            else if (dataBean.getCode() == 5026){
                if (BaseApp.loginAct != null) {
                    if (fragment != null) {
                        fragment.startActivity(new Intent(fragment.getContext(), BaseApp.loginAct));
                        ActivityController.getInstance().closeActNot(BaseApp.loginAct);
                    }else if (activity != null) {
                        activity.startActivity(new Intent(activity, BaseApp.loginAct));
                        ActivityController.getInstance().closeActNot(BaseApp.loginAct);
                    }
                    if (loginOutListener != null)
                        loginOutListener.onLoginOut();
                }
            } else {
                if (context != null)
                    ToastUtils.s(context,dataBean.getMessage());
                onError(dataBean.getMessage());
            }
        }catch (Exception e){
            onError(e.toString());
            Log.e("==``",e.toString());
//            ToastUtils.show(e.toString());
        }
    }

    public abstract void onResponse(M data) throws Exception;

    public abstract void onError(String str);

    public interface LoginOutListener{
        void onLoginOut();
    }
}
